package com.sc.nft.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.sc.nft.dao.MallProductDao;
import com.sc.nft.dao.SupplierManageBindingDao;
import com.sc.nft.dao.SupplierManageDao;
import com.sc.nft.entity.MallProduct;
import com.sc.nft.entity.SupplierManageBinding;
import com.sc.nft.entity.dto.SupplierManageBindingEditDTO;
import com.sc.nft.entity.dto.SupplierManageBindingSaveDTO;
import com.sc.nft.entity.dto.SupplierManageMallBindingDTO;
import com.sc.nft.entity.dto.SupplierManagePageVO;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.SupplierManageBindingService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 供应商和商品绑定(SupplierManageBinding)表服务实现类
 *
 * @author 王伟成
 * @since 2023-10-17 15:29:50
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class SupplierManageBindingServiceImpl implements SupplierManageBindingService {

    private final SupplierManageBindingDao supplierManageBindingDao;
    private final SupplierManageDao supplierManageDao;
    private final MallProductDao mallProductDao;
    @Override
    public void save(SupplierManageBindingSaveDTO supplierManageBindingSaveDTO) {
        MallProduct mallProduct = mallProductDao.getById(supplierManageBindingSaveDTO.getMallId());
        Long mallDaoId = mallProduct.getDaoId();
        SupplierManagePageVO supplierManagePageVO = supplierManageDao.getById(supplierManageBindingSaveDTO.getSupplierId());
        // 2023-10-20日 测试陈鹏和产品蔡懋建确认,不同的dao不允许绑定
        if (!supplierManagePageVO.getDaoId().equals(mallDaoId)) {
            throw new GlobalRunTimeException("不同dao不可以绑定");
        }
        SupplierManageBinding supplierManageBinding = new SupplierManageBinding();
         BeanUtil.copyProperties(supplierManageBindingSaveDTO,supplierManageBinding);
         supplierManageBindingDao.save(supplierManageBinding);
    }
    @Override
    public void edit(SupplierManageBindingEditDTO supplierManageBindingEditDTO) {
        SupplierManageBinding supplierManageBinding = supplierManageBindingDao.getMallId(supplierManageBindingEditDTO.getMallId());
        supplierManageBinding.setSupplierId(supplierManageBindingEditDTO.getSupplierId());
        supplierManageBinding.updateById();
    }
    @Override
    public void mallBinding(SupplierManageMallBindingDTO supplierManageMallBindingDTO) {
        SupplierManageBinding mallId = supplierManageBindingDao.getMallId(supplierManageMallBindingDTO.getMallId());
        MallProduct mallProduct = mallProductDao.getById(supplierManageMallBindingDTO.getMallId());
        Long mallDaoId = mallProduct.getDaoId();
        SupplierManagePageVO supplierManagePageVO = supplierManageDao.getById(supplierManageMallBindingDTO.getSupplierId());
        // 2023-10-20日 测试陈鹏和产品蔡懋建确认,不同的dao不允许绑定
        if (!supplierManagePageVO.getDaoId().equals(mallDaoId)) {
            throw new GlobalRunTimeException("不同dao不可以绑定");
        }
        if (BeanUtil.isEmpty(mallId)) {
            SupplierManageBinding supplierManageBinding = new SupplierManageBinding();
            BeanUtil.copyProperties(supplierManageMallBindingDTO,supplierManageBinding);
            supplierManageBindingDao.save(supplierManageBinding);
        }else {
            SupplierManageBinding supplierManageBinding = new SupplierManageBinding();
            BeanUtil.copyProperties(supplierManageMallBindingDTO,supplierManageBinding);
            supplierManageBinding.setId(mallId.getId());
            supplierManageBinding.updateById();
        }
    }
}
